package com.borqsprocessmanager.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.Constant;
import com.borqsprocessmanager.R;
import com.borqsprocessmanager.adapter.SampleAdapter.OnItemButtonClickListener;
import com.borqsprocessmanager.adapter.SampleAdapter.ViewHolder;
import com.borqsprocessmanager.adapter.SampleAdapter.WarnViewHolder;
import com.borqsprocessmanager.info.ProcessStatsInfo;
import com.borqsprocessmanager.info.SectionInfo;
import com.borqsprocessmanager.util.Log;

public class WhiteAdapter extends SectionAdapter {
    final String TAG = "WhiteAdapter";

    public interface OnItemClickListener {
        public void onItemClick(View v, int posation);
    }

    private OnItemClickListener mOnItemClickListener;

    private Context mContext;
    private ArrayList<ProcessStatsInfo> mList;
    private LayoutInflater mInflater;
    private PackageManager pm;

    public WhiteAdapter(Context context, ArrayList<ProcessStatsInfo> list) {
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(context);
        pm = context.getPackageManager();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ProcessStatsInfo info = mList.get(position);
        Log.v(TAG, "getView_info.type=" + info.type);
        convertView = createProcessItem(convertView, info, position);
        return convertView;

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    private View createProcessItem(View convertView, ProcessStatsInfo info,
            int position) {
        ViewHolder holder;

        // if (convertView == null) {
        convertView = mInflater.inflate(R.layout.white_list_item, null);
        holder = new ViewHolder();
        holder.icon = (ImageView) convertView
                .findViewById(R.id.res_item_image_icon);
        holder.appName = (TextView) convertView
                .findViewById(R.id.res_item_text_name);

        try {
            holder.icon.setImageDrawable(pm
                    .getApplicationIcon(info.packageName));
        } catch (NameNotFoundException e) {
            holder.icon.setImageDrawable(App.mContext.getResources()
                    .getDrawable(R.drawable.android));
        }
        holder.appName.setText(info.appName);

        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        ImageButton actionBtn;
        TextView appName;
        TextView cpuType;
        TextView netType;
    }

    static class WarnViewHolder {
        ImageView icon;
        ImageButton actionBtn;
        TextView title;
        TextView data;
    }

    @Override
    public SectionInfo getSectionInfo(int index) {
        // TODO Auto-generated method stub
        return mList.get(index);
    }

    @Override
    public void remove(int posaction) {
        if (mList != null && mList.size() > 0) {
            mList.remove(posaction);
            Log.v(TAG, "mList===" + mList.size());
        }
    }

    @Override
    public String getHeadTitle(int posaction) {
        // TODO Auto-generated method stub
        return null;
    }
}
